package com.tszs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.FriendshipLinkBiz;
import com.tszs.biz.SmsConfigurationBiz;
import com.tszs.entity.FriendshipLink;
import com.tszs.entity.FriendshipLinkExample;
import com.tszs.entity.SmsConfiguration;
import com.tszs.entity.SmsConfigurationExample;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;

import net.sf.json.JSONObject;

/**
 * @类名:FriendshipLinkController
 * @类描述：友情链接
 * @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 * @author Administrator
 *
 */
@Controller
public class FriendshipLinkController {
	/**
	 * @FriendshipLinkBiz
	 * @描述:分销配置
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private FriendshipLinkBiz friendshipLinkBiz;
	@Autowired
	private SmsConfigurationBiz smsConfigurationBiz;
	

	/**
	 * @方法名:FriendshipLinkInsert
	 * @方法描述：友情链接添加
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/FriendshipLinkInsert")
	public void FriendshipLinkInsert(HttpServletRequest request,HttpServletResponse response) {
		FriendshipLink example=new FriendshipLink();
		example.setfId(Uuid.getUUID());
		example.setfName(request.getParameter("linkName"));
		example.setfPath(request.getParameter("linkAddress"));
		example.setfStatic("0");
		example.setfCreateTime(Uuid.getTime());
		example.setUserName(request.getSession().getAttribute("aUsername")+"");
		example.setUserId(request.getSession().getAttribute("aId")+"");
		int insertSelective=friendshipLinkBiz.insertSelective(example);
		if(insertSelective>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	/**
	 * @方法名:FriendshipLinkUpdate
	 * @方法描述：友情链接修改
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/FriendshipLinkUpdate")
	public void FriendshipLinkUpdate(HttpServletRequest request,HttpServletResponse response) {
		FriendshipLink example=new FriendshipLink();
		example.setfName(request.getParameter("linkName"));
		example.setfPath(request.getParameter("linkAddress"));
		example.setfStatic("0");
		example.setfCreateTime(Uuid.getTime());
		example.setUserName(request.getSession().getAttribute("aUsername")+"");
		example.setUserId(request.getSession().getAttribute("aId")+"");
		FriendshipLinkExample friendshipLinkExample=new FriendshipLinkExample();
		com.tszs.entity.FriendshipLinkExample.Criteria ca=friendshipLinkExample.createCriteria();
		ca.andFIdEqualTo(request.getParameter("id"));
		int insertSelective=friendshipLinkBiz.updateByExampleSelective(example, friendshipLinkExample);
		if(insertSelective>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	
	/**
	 * 保存在线客服配置
	 * @author Fujing
	 * @date 2019年5月31日 下午4:16:19
	 * @param request
	 * @param response void
	 */
	@RequestMapping("pc/admin/handinOnlieServiceSetting")
	public void handinOnlieServiceSetting(HttpServletRequest request, HttpServletResponse response) {
		SmsConfiguration example = new SmsConfiguration();
		example.setsConten(request.getParameter("sConten"));
		String sId = request.getParameter("sId");
		int res = 0;
		if(StringUtils.isNotEmpty(sId)) {
			SmsConfigurationExample sms = new SmsConfigurationExample();
			com.tszs.entity.SmsConfigurationExample.Criteria ca = sms.createCriteria();
			ca.andSIdEqualTo(sId);
			res = smsConfigurationBiz.updateByExampleSelective(example, sms);
		} else {
			example.setsId(Uuid.getUUID());
			example.setsCreateTime(Uuid.getTime());
			example.setsPass("在线客服");
			res = smsConfigurationBiz.insertSelective(example);
		}
		if(res>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	
	/**
	 * @方法名:FriendshipLinkpage
	 * @方法描述：友情链接分页
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/FriendshipLinkpage")
	public void FriendshipLinkpage(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		FriendshipLink example = new FriendshipLink();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		List<FriendshipLink> selectByExample = friendshipLinkBiz.selectByExamplePage(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("fId",i+1);
			mapData.put("fName", selectByExample.get(i).getfName());
			mapData.put("fPath", selectByExample.get(i).getfPath());
			mapData.put("fCreateTime",selectByExample.get(i).getfCreateTime());
			mapData.put("UserId", selectByExample.get(i).getUserId());
			mapData.put("UserName", selectByExample.get(i).getUserName());
			mapData.put("button",
					"<a href=\"javascript:table('1','"
							+ selectByExample.get(i).getfId()
							+ "')\" class=\"layui-btn layui-btn-danger layui-btn-xs\" style=''>修改</a> <a href=\"javascript:table('2','"
							+ selectByExample.get(i).getfId() + "')\" class=\"layui-btn layui-btn-xs\">删除</a>");
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}
	/**
	 * @方法名:FriendshipLinkUpdatefStatic
	 * @方法描述：伪删除
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/FriendshipLinkUpdatefStatic")
	public void FriendshipLinkUpdatefStatic(HttpServletRequest request,HttpServletResponse response) {
		FriendshipLink example=new FriendshipLink();
		example.setfStatic("1");
		FriendshipLinkExample friendshipLinkExample=new FriendshipLinkExample();
		com.tszs.entity.FriendshipLinkExample.Criteria ca=friendshipLinkExample.createCriteria();
		ca.andFIdEqualTo(request.getParameter("id"));
		int insertSelective=friendshipLinkBiz.updateByExampleSelective(example, friendshipLinkExample);
		if(insertSelective>0) {
			new CallBack().yes(response, "", "");
		}else {
			new CallBack().no(response, "", "");
		}
	}
	/**
	 * @方法名:findFriendyLinksById
	 * @方法描述：查询修改的ID值
	 * @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/findFriendyLinksById")
	public void findFriendyLinksById(HttpServletRequest request,HttpServletResponse response) {
		FriendshipLinkExample example=new FriendshipLinkExample();
		com.tszs.entity.FriendshipLinkExample.Criteria ca=example.createCriteria();
		ca.andFIdEqualTo(request.getParameter("id"));
		List<FriendshipLink>selectByExamplee=friendshipLinkBiz.selectByExample(example);
		if(selectByExamplee.size()>0) {
			new CallBack().yes(response, selectByExamplee, "");
		}else {
			new CallBack().no(response, selectByExamplee, "");
		}
	}
}
